class ContactsController < ApplicationController
  before_filter :login_required, :except => [:subscribe, :do_subscribe, :activate, :unsubscribe]
  layout 'default'
  
  # GET /contacts
  # GET /contacts.xml
  def index
    args = {}
    args[:order] = params[:sort] or :created_at
    @contacts = Contact.all(args)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @contacts }
    end
  end

  # GET /contacts/1
  # GET /contacts/1.xml
  def show
    @contact = Contact.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @contact }
    end
  end

  # GET /contacts/new
  # GET /contacts/new.xml
  def new
    @contact = Contact.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @contact }
    end
  end

  # GET /contacts/1/edit
  def edit
    @contact = Contact.find(params[:id])
  end

  # POST /contacts
  # POST /contacts.xml
  def create
    @contact = Contact.new(params[:contact])

    respond_to do |format|
      if @contact.save
        flash[:notice] = render_to_string :partial => "created_notice"
        format.html { redirect_to(@contact) }
        format.xml  { render :xml => @contact, :status => :created, :location => @contact }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @contact.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /contacts/1
  # PUT /contacts/1.xml
  def update
    @contact = Contact.find(params[:id])

    respond_to do |format|
      if @contact.update_attributes(params[:contact])
        flash[:notice] = 'Contact was successfully updated.'
        format.html { redirect_to(@contact) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @contact.errors, :status => :unprocessable_entity }
      end
    end
  end

  # GET /subscribe
  # Display a subscription form for the contact to enter his email
  def subscribe
    @contact = Contact.new
  end

  # POST /contacts/do_subscribe save the new contact (from subscribe)
  def do_subscribe
    @contact = Contact.new(params[:contact])

    # Don't active contact just yet, we'll send an activation email
    @contact.active = false

    if @contact.save
      flash[:notice] = 'Thank you! Please check your email to activate your subscription.'
      ContactActivation.deliver_activate(@contact)
    else
      render :action => "subscribe"
    end
  end
  
  # GET /activate_subscription/:token
  # activate contact based on token
  def activate
    flash[:notice] = nil
    
    @contact = Contact.find_by_auth_token(params[:token])

    if @contact
      @contact.active = true
      @contact.save
    end
  end
  
  # GET /unsubscribe/:token
  # unsubscribe (inactivate) contact based on token
  def unsubscribe
    @contact = Contact.find_by_auth_token(params[:token])

    if @contact
      @contact.active = false
      @contact.save
    end
  end

  # DELETE /contacts/1
  # DELETE /contacts/1.xml
  def destroy
    @contact = Contact.find(params[:id])
    @contact.destroy

    respond_to do |format|
      format.html { redirect_to(contacts_url) }
      format.xml  { head :ok }
    end
  end
end
